﻿using DocumentFormat.OpenXml.Drawing.Charts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MicroLED_Detecter
{
    public class Spectrometer
    {
        // 每次声明一个外部函数时都需要使用 DllImport("NewSpecLib")，因为这个特性告诉了编译器在哪里可以找到被调用的函数
        [DllImport("NewSpecLib")]
        private static extern int BQ_Connect();

        [DllImport("NewSpecLib")]
        private static extern void BQ_Disconnect();

        [DllImport("NewSpecLib")]
        private static extern void BQ_Set_IntTime(int Value);

        [DllImport("NewSpecLib")]
        private static extern void BQ_Set_BCW(int Value);

        [DllImport("NewSpecLib")]
        private static extern void BQ_Set_STA(int Value);

        [DllImport("NewSpecLib")]
        private static extern int BQ_GetPixelCount(int index);

        [DllImport("NewSpecLib")]
        private static extern void BQ_GetWaveLength(int index, [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_R8)] ref double[] wl);

        [DllImport("NewSpecLib")]
        private static extern void BQ_GetSerialNo(int index, int iByte, ref Byte sByte);

        [DllImport("NewSpecLib")]
        private static extern void BQ_DoScan();

        [DllImport("NewSpecLib")]
        private static extern void BQ_GetSpectrum([MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_R8)] ref double[] spec);

        [DllImport("NewSpecLib")]
        private static extern void BQ_SetupAbsoluteIrr(int Index, [MarshalAs(UnmanagedType.LPStr)] string CorrFile, [MarshalAs(UnmanagedType.LPStr)] string LampFile,
    double CollectionArea, int IntSphere, int ObserverIndex, [MarshalAs(UnmanagedType.LPStr)] string Illuminant);

        [DllImport("NewSpecLib")]
        private static extern void BQ_SetDark(int index);

        [DllImport("NewSpecLib")]
        private static extern void BQ_SetRef(int index);

        public int ConnectSpectrometer()   // 连接光谱仪
        {
            int ret = 0;
            try
            {
                ret = BQ_Connect(); // 成功返回0，1表示没有找到光谱仪，2表示光谱仪未授权
                if (ret == 1)
                    MessageBox.Show("No spectrometer found");
                return ret;
            }
            catch(Exception ex)
            {               
                MessageBox.Show(ex.Message, "Error");
            }
            return -1;// -1表示出错
        }
        public int DisconnectSpectrometer()    // 断开光谱仪
        {
            try
            {
                BQ_Disconnect();
                return 1;// 1表示断开成功
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
            return 0;
        }
        public int SetIntTime(int Value)   // 设置积分时间 ms
        {
            try
            {
                BQ_Set_IntTime(Value);
                return 1;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
            return 0;
        }
        public int SetBCW(int Value) // 设置平滑宽度
        {
            try
            {
                BQ_Set_BCW(Value);
                return 1;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
            return 0;
        }
        public int SetSTA(int Value)   // 设置采样次数
        {
            try
            {
                BQ_Set_STA(Value);
                return 1;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
            return 0;
        }
        public int GetPixelCount(int index) // 获取像素点数量
        {
            return BQ_GetPixelCount(index);
        }
        public void GetWaveLength(int index, ref double[] wl)   // 获取波长
        {
            BQ_GetWaveLength(index, ref wl);
        }

        //public string GetSerialNo(int index)    // 获取序列号
        //{
        //    try
        //    {
        //        string s = "";
        //        byte B = 0;
        //        int i = 1;
        //        do
        //        {
        //            BQ_GetSerialNo(index, i, ref B);
        //            if (B != 0)
        //                s += System.Convert.ToChar(B);
        //            i++;
        //        }
        //        while (B != 0);
        //        return s;
        //    }
        //    catch
        //    {
        //        return "";
        //    }
        //}

        public void DoScan()    // 测量光谱
        {
            BQ_DoScan();
        }
        public void GetSpectrum(ref double[] spec)  // 获取光谱
        {
            BQ_GetSpectrum(ref spec);
        }
        public void SetupAbsoluteIrr(int Index, string CorrFile, string LampFile, double CollectionArea, int IntSphere, int ObserverIndex, string Illuminant)   // 绝对能量校正
        {
            BQ_SetupAbsoluteIrr(Index, CorrFile, LampFile, CollectionArea, IntSphere, ObserverIndex, Illuminant);
        }

        public void SetDark(int index)  // 设置当前光谱为暗光谱
        {
            BQ_SetDark(index);
        }

        public void SetRef(int index)   // 设置当前光谱为参考光谱
        {
            BQ_SetRef(index);
        }
    }
}
